#!/bin/sh
flag=0
chmod 777 /mnt/nandflash/install.tar.gz
if [ ! -f /etc/rc.local.NRbat ]
then
	cp -Rf /etc/rc.local /etc/rc.local.NRbat
fi
cp -Rf /etc/rc.local.NRbat /etc/rc.local
if [ ! -d "/mnt/nandflash/app/" ]
then
  mkdir -p /mnt/nandflash/app/
fi
  chmod 777 /mnt/nandflash/app/
if [ ! -d "/mnt/nandflash/download/" ]
then
  mkdir -p /mnt/nandflash/download/
fi
  chmod 777 /mnt/nandflash/download/
if [ ! -d "/mnt/nandflash/back-up/" ]
then
  mkdir -p /mnt/nandflash/back-up/
fi
chmod 777 /mnt/nandflash/back-up/
if [ ! -d "/mnt/nandflash/app/log/" ]
then
  mkdir -p /mnt/nandflash/app/log/
fi
chmod 777 /mnt/nandflash/app/log/
if [ ! -d "/mnt/nandflash/app/data/" ]
then
  mkdir -p /mnt/nandflash/app/data/
fi
chmod 777 /mnt/nandflash/app/data/
if [ ! -d "/mnt/nandflash/app/cache/" ]
then
  mkdir -p /mnt/nandflash/app/cache/
fi
chmod 777 /mnt/nandflash/app/cache/
tar -zvxf /mnt/nandflash/install.tar.gz  -C /mnt/nandflash/
chmod 777 /mnt/nandflash/installapp/*
chmod 777 /mnt/nandflash/installlib/*
chmod 777 /mnt/nandflash/config/*
chmod 777 /mnt/nandflash/image/*
chmod 777 /mnt/nandflash/voice/*
cp -Rf /mnt/nandflash/config/* /mnt/nandflash/app/
if [ -f /mnt/nandflash/app/tcu_main.conf ]
then
 echo "cp tcu_main.conf is successful"
else
 echo "cp tcu_main.conf is failed"
 flag=1
fi
if [ -f /mnt/nandflash/app/tcudata.db ]
then
 echo "cp tcudata.db is successful"
else
 echo "cp tcudata.db is failed"
 flag=1
fi
cp -a /mnt/nandflash/voice/ /opt/
cp -a /mnt/nandflash/image/ /opt/
if [  -f /usr/sbin/smac ]
then
 sed -i '$d' /etc/rc.local
 cp -Rf /mnt/nandflash/installapp/* /mnt/nandflash/app/
 echo "/mnt/nandflash/app/tcu_ppp &" >> /etc/rc.local
 echo "/mnt/nandflash/app/tcu_guarder &" >> /etc/rc.local
   if [ -f /mnt/nandflash/app/tcu_ppp ]
   then
      echo "cp tcu_ppp is successful"
   else
      echo "cp tcu_ppp failed"
      flag=1
   fi
else
 cp /mnt/nandflash/installapp/sftp_client /mnt/nandflash/app/
 cp /mnt/nandflash/installapp/tcu_guarder /mnt/nandflash/app/
 cp /mnt/nandflash/installapp/tcu_hmi /mnt/nandflash/app/
 cp /mnt/nandflash/installapp/tcu_main /mnt/nandflash/app/
 cp /mnt/nandflash/installapp/blacklistconvert /mnt/nandflash/app/
  cp /mnt/nandflash/installapp/whitelistconvert /mnt/nandflash/app/
 cp /mnt/nandflash/installapp/md5Compare /mnt/nandflash/app/
 cp /mnt/nandflash/installapp/tcu_price /mnt/nandflash/app/
 cp /mnt/nandflash/installapp/tcu_alarm /mnt/nandflash/app/
 cp /mnt/nandflash/installapp/tcu_ant /mnt/nandflash/app/
 cp /mnt/nandflash/installapp/tcu_ppp /mnt/nandflash/app/
 echo "/mnt/nandflash/app/tcu_guarder &" >> /etc/rc.local
fi
 echo "exit 0" >> /etc/rc.local
if [ -f /mnt/nandflash/app/sftp_client ]
then
  echo "cp sftp_client is successful"
else
  echo "cp sftp_client failed"
  flag=1
fi
if [ -f /mnt/nandflash/app/tcu_guarder ]
then
  echo "cp tcu_guarder is successful"
else
  echo "cp tcu_guarder failed"
  flag=1
fi
if [ -f /mnt/nandflash/app/tcu_hmi ]
then
  echo "cp tcu_hmi is successful"
else
  echo "cp tcu_hmi failed"
  flag=1
fi
if [ -f /mnt/nandflash/app/tcu_main ]
then
  echo "cp tcu_main is successful"
else
  echo "cp tcu_main failed"
  flag=1
fi
if [ -f /mnt/nandflash/app/tcu_price ]
then
  echo "cp tcu_price is successful"
else
  echo "cp tcu_price failed"
  flag=1
fi
if [ -f /mnt/nandflash/app/tcu_alarm ]
then
  echo "cp tcu_alarm is successful"
else
  echo "cp tcu_alarm failed"
  flag=1
fi
if [ -f /mnt/nandflash/app/blacklistconvert ]
then
  echo "cp blacklistconvert is successful"
else
  echo "cp blacklistconvert failed"
  flag=1
fi
if [ -f /mnt/nandflash/app/md5Compare ]
then
  echo "cp md5Compare is successful"
else
  echo "cp md5Compare failed"
  flag=1
fi
if [ -f /mnt/nandflash/app/tcu_ant ]
then
  echo "cp tcu_ant is successful"
else
  echo "cp tcu_ant failed"
  flag=1
fi
if [ -f /mnt/nandflash/app/whitelistconvert ]
then
  echo "cp whitelistconvert is successful"
else
  echo "cp whitelistconvert failed"
  flag=1
fi
#cp installlib
if [ -f /usr/sbin/smac ]
then
cp -Rf /mnt/nandflash/installlib/* /usr/lib/
   if [ -f /usr/lib/libtcu.so ]
   then
      echo "cp libtcu.so is successful"
   else
      echo "cp libtcu.so is failed"
      flag=1
   fi
else
cp /mnt/nandflash/installlib/libsqlite3.so.0.8.6 /usr/lib/
cp /mnt/nandflash/installlib/libssh2.so.1.0.1 /usr/lib/
cp /mnt/nandflash/installlib/libtcu_reg.so /usr/lib/
cp /mnt/nandflash/installlib/libz.so.1 /usr/lib/
fi
#check file exit
if [ -f /usr/lib/libsqlite3.so.0.8.6 ]
then
echo "cp libsqlite3.so.0.8.6 is successful"
else
echo "cp libsqlite3.so.0.8.6 failed"
flag=1
fi
if [ -f /usr/lib/libssh2.so.1.0.1 ]
then
echo "cp libssh2.so.1.0.1 is successful"
else
echo "cp libssh2.so.1.0.1 failed"
flag=1
fi
if [ -f /usr/lib/libtcu_reg.so ]
then
echo "cp libtcu_reg.so is successful"
else
echo "cp libtcu_reg.so failed"
flag=1
fi
if [ -f /usr/lib/libz.so.1 ]
then
	echo "cp libz.so.1 is successful"
else
	echo "cp libz.so.1 failed"
	flag=1
fi
cp /usr/lib/libsqlite3.so.0.8.6 /usr/lib/libsqlite3.so
if [ -f /usr/lib/libsqlite3.so ]
then
	echo "link libsqlite3.so successful"
else
	echo "link libsqlite3.so failed"
	flag=1
fi
cp /usr/lib/libsqlite3.so.0.8.6 /usr/lib/libsqlite3.so.0
if [ -f /usr/lib/libsqlite3.so.0 ]
then
	echo "link libsqlite3.so.0 successful"
else
	echo "link libsqlite3.so.0 failed"
	flag=1
fi
cp /usr/lib/libssh2.so.1.0.1 /usr/lib/libssh2.so
if [ -f /usr/lib/libssh2.so ]
then
	echo "link libssh2.so successful"
else
	echo "link libssh2.so failed"
	flag=1
fi
cp /usr/lib/libssh2.so.1.0.1 /usr/lib/libssh2.so.1
if [ -f /usr/lib/libssh2.so.1 ]
then
	echo "link libssh2.so.1 successful"
else
	echo "link libssh2.so.1 failed"
	flag=1
fi
cp /usr/lib/libz.so.1 /usr/lib/libz.so
if [ -f /usr/lib/libz.so ]
then
	echo "link libz.so successful"
else
	echo "link libz.so failed"
	flag=1
fi

rm -Rf /mnt/nandflash/config/
rm -Rf /mnt/nandflash/image/
rm -Rf /mnt/nandflash/installapp/
rm -Rf /mnt/nandflash/installlib/
rm -Rf /mnt/nandflash/voice/
rm /mnt/nandflash/install.tar.gz
if [ $flag == 0 ]
then
	echo " =============================================="
	echo "              INSTALL SUCCESSFUL!             "
	echo " =============================================="
else
	echo " =============================================="
	echo "                INSTALL FAILED!              "
	echo " =============================================="
fi
